<?php
class BookCourseController extends AdminBaseController  {
	public function studentCourseList(){
		$where=$_GET;
		$where['user_id'] = $where['user_id'];
		$where['class_status'] =CLASS_STATUS_2;
		$data = HttpServerManager::getBookCourseServer()->lists($where);
		$data['booktime_pair'] = Common::getTimesPairsArray();
		$data['bookTimeOptions'] = page_modifier_options($data['booktime_pair'],common::getGetStrValue('book_time'));
		
		$data['book_teacher_status_pair'] = Common::_t('book_teacher_status');
		$data['user_communication_pair'] = Common::_t('user_communication');
		$data['user_job_pair'] = Common::_t('user_job');
		
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$data['level_pair'] = $level_pair;
		$data['teacherStatusOptions'] = page_modifier_options($data['book_teacher_status_pair'],common::getGetStrValue('teacher_status','-1'));
		return $data;
	}
	
	public function teacherGetList(){
		$where=$_GET;
		$where['teacher_id'] = $_SESSION['admin']['teacher_id'];
		$where['class_status_teacher'] = 1;
		$data = HttpServerManager::getBookCourseServer()->lists($where);
		$data['booktime_pair'] = Common::getTimesPairsArray();
		$data['bookTimeOptions'] = page_modifier_options($data['booktime_pair'],common::getGetStrValue('book_time'));
		
		$data['book_teacher_status_pair'] = Common::_t('book_teacher_status');
		$data['user_communication_pair'] = Common::_t('user_communication');
		$data['user_job_pair'] = Common::_t('user_job');
		
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$data['level_pair'] = $level_pair;
		$data['teacherStatusOptions'] = page_modifier_options($data['book_teacher_status_pair'],common::getGetStrValue('teacher_status','-1'));
		$data['refer_url'] = 'bookcourse_teacherGetList';		
		Route::$template = WEB_DIR.DIRECTORY_SEPARATOR.'template' .DIRECTORY_SEPARATOR.Route::$work_dir. DIRECTORY_SEPARATOR.'bookCourse'.DIRECTORY_SEPARATOR.'teacherManagerList.php';
		return $data;
	}
	
	public function teacherManagerList(){
		$where=$_GET;
		$where['class_status_teacher'] = 1;
		$data = HttpServerManager::getBookCourseServer()->lists($where);
		$data['booktime_pair'] = Common::getTimesPairsArray();
		$data['bookTimeOptions'] = page_modifier_options($data['booktime_pair'],common::getGetStrValue('book_time'));
		
		$data['book_teacher_status_pair'] = Common::_t('book_teacher_status');
		$data['user_communication_pair'] = Common::_t('user_communication');
		$data['user_job_pair'] = Common::_t('user_job');
		
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$data['level_pair'] = $level_pair;
		$data['teacherStatusOptions'] = page_modifier_options($data['book_teacher_status_pair'],common::getGetStrValue('teacher_status','-1'));
		Route::$template = WEB_DIR.DIRECTORY_SEPARATOR.'template' .DIRECTORY_SEPARATOR.Route::$work_dir. DIRECTORY_SEPARATOR.'bookCourse'.DIRECTORY_SEPARATOR.'teacherManagerList.php';
		return $data;

	}
	
	public function lists(){
		$data = HttpServerManager::getBookCourseServer()->lists($_GET);
		$data['booktime_pair'] = Common::getTimesPairsArray();
		$data['bookTimeOptions'] = page_modifier_options($data['booktime_pair'],common::getGetStrValue('book_time'));
		
		$data['book_teacher_status_pair'] = Common::_t('book_teacher_status');
		$data['user_communication_pair'] = Common::_t('user_communication');
		$data['user_job_pair'] = Common::_t('user_job');
		
		$level_list = HttpDaoManager::getLevelDao()->searchList();
		$level_pair = array();
		foreach($level_list as $k=>$v){
			if(isset($v['id'])){
				$level_pair[$v['id']]='Level'.'-'.$v['level'];
			}
		}
		unset($level_list);
		$data['refer_url'] = 'bookcourse_lists';
		$data['level_pair'] = $level_pair;
		$data['teacherStatusOptions'] = page_modifier_options($data['book_teacher_status_pair'],common::getGetStrValue('teacher_status','-1'));
		return $data;
	}
	/**
	 * 保存
	 */
	public function saveRemark(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];
		$info = HttpDaoManager::getBookCourseDao()->get($id);
		$remark ="";
		$log_type=BOOK_COURSE_LOG_TYPE_5;
		
		$info_new = array();
		$info_new['report_status'] = $_POST['report_status'];
		if($info['report_status']!=$_POST['report_status']){
			$reportStatus = Common::_t('teacher_report_status');
			$remark .= "老师报道状态由:".$reportStatus[$info['report_status']]."更新为:".$reportStatus[$_POST['report_status']]."<br>";
		}
		
		$info_new['teacher_status'] = $_POST['teacher_status'];
		if($info['teacher_status']!=$_POST['teacher_status']){
			$teacherStatus = Common::_t('book_teacher_status');
			$remark .= "老师上课状态由:".$teacherStatus[$info['teacher_status']]."更新为:".$teacherStatus[$_POST['teacher_status']]."<br>";
		}
		
		$info_new['user_status'] = $_POST['user_status'];
		if($info['user_status']!=$_POST['user_status']){
			$userStatus = Common::_t('book_user_status');
			$remark .= "学生上课状态由:".$userStatus[$info['user_status']]."更新为:".$userStatus[$_POST['user_status']]."<br>";
		}
		
		$info_new['substitute'] = $_POST['substitute'];
		if($info['substitute']!=$_POST['substitute']){
			$substitueStatus = Common::_t('teacher_substitute');
			$remark .= "代课状态由:".$substitueStatus[$info['substitute']]."更新为:".$substitueStatus[$_POST['substitute']]."<br>";
				
		}
		if($info['teacher_id']!=$_POST['teacher_id']){
			$teacher_id_list = array($info['teacher_id'],$_POST['teacher_id']);
			$teacherList =HttpDaoManager::getTeacherDao()->getDataPairList($teacher_id_list);
			$teacherPair = array();
			foreach($teacherList as $key=>$value){
				$teacherPair[$value['id']]=$value['name'].'-'.$value['serial_number'];
			}
			$remark .= "老师变更由:".$teacherPair[$info['teacher_id']]."更新为:".$teacherPair[$_POST['teacher_id']]."<br>";
			//老师发生变化
			$info_new['teacher_id'] = $_POST['teacher_id'];//现在ID
			$new_teacher_class_info = HttpDaoManager::getTeacherStartCourseDao()->getByTeacherId($info_new['teacher_id'],$info['book_date'],$info['book_time']);
			$info_new['original_teacher_id'] = $_POST['old_teacher_id'];//旧ID
			$info_new['teacher_class_id'] =$new_teacher_class_info['id'];//获取最新的teacher_class_id
			if($_POST['substitute']==BOOK_SUBSTITUTE_1){
				$info_new['original_teacher_id']=0;//没法生变化，旧老师依然为0
				if($info['teacher_id']!=$_POST['teacher_id']){//代课老师恢复
					$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_NOTBOOKING);//恢复
					HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
				}
			}else{
				$info_new['original_teacher_id']=$_POST['old_teacher_id'];
				if($_POST['teacher_id']!=$info['teacher_id']){//代课老师
					if($info['original_teacher_id']==0){//代课情况
						if($info_new['substitute']==BOOK_SUBSTITUTE_2){
							$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_ABSENTCLASS);//原老师缺课
							HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
						}
						if($info_new['substitute']==BOOK_SUBSTITUTE_3){
							$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_LEAVECLASS);//原老师请假
							HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
						}
					}else{//代课老师互相替换情况
						$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_NOTBOOKING);//代课老师替换后,原代课老师恢复
						HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);					
					}
			  }
			}
		}else{
			if($info_new['substitute']!=$info['substitute']){//发生变化
				$old_teacher_class_info = HttpDaoManager::getTeacherStartCourseDao()->getByTeacherId($info['original_teacher_id'],$info['book_date'],$info['book_time']);
				if($info_new['substitute']==BOOK_SUBSTITUTE_2){
					$data = array('id'=>$old_teacher_class_info['id'],'status'=>TEACHER_CLASS_STATUS_ABSENTCLASS);//原老师缺课
					HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
				}
				if($info_new['substitute']==BOOK_SUBSTITUTE_3){
					$data = array('id'=>$old_teacher_class_info['id'],'status'=>TEACHER_CLASS_STATUS_LEAVECLASS);//原老师请假
					HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
				}
			}
		}
		


		if(!empty($remark)){
			HttpServerManager::getBookCourseServer()->updateTeacherAndStudenStatus($id,$info_new,$remark,$log_type);
		}

		$info2 = HttpDaoManager::getBookCourseDao()->get($id);

		//记录上课老师的状态
		if($info2['teacher_status']==BOOK_TEACHER_STATUS_2){
			if($info2['substitute']==BOOK_SUBSTITUTE_1){
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_CLASSED);
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//正常授课
			}else{
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_SUBSTITUECLASS);
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//正常授课				
			}
		}else{
			if($info2['report_status']==BOOK_REPORT_STATUS_3){
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_ABSENTCLASS);//缺课
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
			}else{
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_SUBSTITUECLASS);
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//正常代课				
			}
		}
		
		HttpServerManager::getBookCourseServer()->saveRemark($id,$_POST['remark']);//记录备注
		$msg = Common::_t('msg');
		ClassManager::getToos()->msgbox($msg[MESSAGE_TYPE_7]);
	}	
	
	/**
	 * 完成
	 */
	public function complete(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];

		$info = HttpDaoManager::getBookCourseDao()->get($id);
		$remark ="";
		$log_type=BOOK_COURSE_LOG_TYPE_2;
			
			$info_new = array();
		$info_new['report_status'] = $_POST['report_status'];
		if($info['report_status']!=$_POST['report_status']){
			$reportStatus = Common::_t('teacher_report_status');
			$remark .= "老师报道状态由:".$reportStatus[$info['user_status']]."更新为:".$reportStatus[$_POST['report_status']]."<br>";
		}
		
		$info_new['teacher_status'] = $_POST['teacher_status'];
		if($info['teacher_status']!=$_POST['teacher_status']){
			$teacherStatus = Common::_t('book_teacher_status');
			$remark .= "老师上课状态由:".$teacherStatus[$info['teacher_status']]."更新为:".$teacherStatus[$_POST['teacher_status']]."<br>";
		}
		
		$info_new['user_status'] = $_POST['user_status'];
		if($info['user_status']!=$_POST['user_status']){
			$userStatus = Common::_t('book_user_status');
			$remark .= "学生上课状态由:".$userStatus[$info['user_status']]."更新为:".$userStatus[$_POST['user_status']]."<br>";
		}
		
		$info_new['substitute'] = $_POST['substitute'];
		if($info['substitute']!=$_POST['substitute']){
			$substitueStatus = Common::_t('teacher_substitute');
			$remark .= "代课状态由:".$substitueStatus[$info['substitute']]."更新为:".$substitueStatus[$_POST['substitute']]."<br>";
				
		}
		if($info['teacher_id']!=$_POST['teacher_id']){//1变为2,3肯定老师发生变化
			$teacher_id_list = array($info['teacher_id'],$_POST['teacher_id']);
			$teacherList =HttpDaoManager::getTeacherDao()->getDataPairList($teacher_id_list);
			$teacherPair = array();
			foreach($teacherList as $key=>$value){
				$teacherPair[$value['id']]=$value['name'].'-'.$value['serial_number'];
			}
			$remark .= "老师变更由:".$teacherPair[$info['teacher_id']]."更新为:".$teacherPair[$_POST['teacher_id']]."<br>";
			
			$info_new['teacher_id'] = $_POST['teacher_id'];//现在ID
			$new_teacher_class_info = HttpDaoManager::getTeacherStartCourseDao()->getByTeacherId($info_new['teacher_id'],$info['book_date'],$info['book_time']);
			$info_new['original_teacher_id'] = $_POST['old_teacher_id'];//旧ID
			$info_new['teacher_class_id'] =$new_teacher_class_info['id'];//获取最新的teacher_class_id
			if($_POST['substitute']==BOOK_SUBSTITUTE_1){
				$info_new['original_teacher_id']=0;//没法生变化，旧老师依然为0
				if($info['teacher_id']!=$_POST['teacher_id']){//代课老师恢复
					$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_NOTBOOKING);//恢复
					HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
				}
			}else{
				$info_new['original_teacher_id']=$_POST['old_teacher_id'];
				if($_POST['teacher_id']!=$info['teacher_id']){//代课老师
					if($info['original_teacher_id']==0){//代课情况
						if($info_new['substitute']==BOOK_SUBSTITUTE_2){
							$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_ABSENTCLASS);//原老师缺课
							HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
						}
						if($info_new['substitute']==BOOK_SUBSTITUTE_3){
							$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_LEAVECLASS);//原老师请假
							HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
						}
					}else{//代课老师互相替换情况
						$data = array('id'=>$info['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_NOTBOOKING);//代课老师替换后,原代课老师恢复
						HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
					}
				}
			}
				
		}else{//代课老师不变，2<>3变化
			if($info_new['substitute']!=$info['substitute']){//发生变化
				$old_teacher_class_info = HttpDaoManager::getTeacherStartCourseDao()->getByTeacherId($info['original_teacher_id'],$info['book_date'],$info['book_time']);
				if($info_new['substitute']==BOOK_SUBSTITUTE_2){
					$data = array('id'=>$old_teacher_class_info['id'],'status'=>TEACHER_CLASS_STATUS_ABSENTCLASS);//原老师缺课
					HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
				}
				if($info_new['substitute']==BOOK_SUBSTITUTE_3){
					$data = array('id'=>$old_teacher_class_info['id'],'status'=>TEACHER_CLASS_STATUS_LEAVECLASS);//原老师请假
					HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
				}
			}
		}
		
		
		if(!empty($remark)){
			HttpServerManager::getBookCourseServer()->updateTeacherAndStudenStatus($id,$info_new,$remark,$log_type);
		}

		$info2 = HttpDaoManager::getBookCourseDao()->get($id);
		//记录上课老师的状态
		if($info2['teacher_status']==BOOK_TEACHER_STATUS_2){
			if($info2['substitute']==BOOK_SUBSTITUTE_1){
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_CLASSED);
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//正常授课
			}else{
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_SUBSTITUECLASS);
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//正常授课				
			}
		}else{
			if($info2['report_status']==BOOK_REPORT_STATUS_3){
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_ABSENTCLASS);//缺课
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);
			}else{
				$data = array('id'=>$info2['teacher_class_id'],'status'=>TEACHER_CLASS_STATUS_SUBSTITUECLASS);
				HttpDaoManager::getTeacherStartCourseDao()->updateStatus($data['status'],$data['id']);//正常代课				
			}
		}

		HttpServerManager::getBookCourseServer()->completeComplaint($id,$_POST['remark']);
		//此处若状态发生变化,完成过为处理投诉订单时候的完成

		$msg = Common::_t('msg');
		ClassManager::getToos()->msgbox($msg[MESSAGE_TYPE_4]);
	}

	/**
	 * 取消
	 */
	public function cancellation(){
		$id = isset($_GET['id'])?$_GET['id']:$_POST['id'];
		$tmp = HttpServerManager::getBookCourseServer()->adminCancellation($id,$_POST['remark'],-1);
		$msg = Common::_t('msg');
		if ($tmp===true){
			ClassManager::getToos()->msgbox($msg[MESSAGE_TYPE_6]);
		}
		else{
			ClassManager::getToos()->msgbox($msg[$tmp]);
		}
	}

	public function info(){
		
		$ret_data =HttpServerManager::getBookCourseServer()->bookCourseDetailInfo($_GET['id']); 
		if (!empty($_SESSION['admin']['teacher_id'])) {
			Route::$template = WEB_DIR.DIRECTORY_SEPARATOR.'template' .DIRECTORY_SEPARATOR.'eos'.DIRECTORY_SEPARATOR.'bookCourse'.DIRECTORY_SEPARATOR.'info.php';
		}
		return $ret_data;
	}
	
	public function getOrder(){
		$target = $_POST['target'];
		$where = array();
		$where['book_no'] = $target;
		$orderList = HttpDaoManager::getBookCourseDao()->searchList($where);

		$result= array();
		if(empty($orderList)){//找不到
			$result = array('success'=>'fail','num'=>0)	;
			$where['email'] = $user_name;
		}else{//找到了
			$num = count($orderList);
			if($num>1){
				$result = array('success'=>'fail','num'=>$num);
			}
			$result = array('success'=>'success','num'=>$num,'order_info'=>$orderList[0]);
		}
		
		exit(json_encode($result));
	}
}
?>